package com.sokal.easycare.modules.service.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.service.entity.SerService;
import com.sokal.easycare.modules.service.service.SerServiceService;

/**
 * 服务Controller
 * @author liliangdong
 * @version 2017-03-27
 */
@Controller
@RequestMapping(value = "${adminPath}/service/serService")
public class SerServiceController extends BaseController {

	@Autowired
	private SerServiceService serServiceService;
	
	@ModelAttribute
	public SerService get(@RequestParam(required=false) String id) {
		SerService entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = serServiceService.get(id);
		}
		if (entity == null){
			entity = new SerService();
		}
		return entity;
	}
	
	@RequiresPermissions("service:serService:view")
	@RequestMapping(value = {"list", ""})
	public String list(SerService serService, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SerService> page = serServiceService.findPage(new Page<SerService>(request, response), serService); 
		model.addAttribute("page", page);
		return "modules/service/serServiceList";
	}

	@RequiresPermissions("service:serService:view")
	@RequestMapping(value = "form")
	public String form(SerService serService, Model model) {
		model.addAttribute("serService", serService);
		return "modules/service/serServiceForm";
	}

	@RequiresPermissions("service:serService:edit")
	@RequestMapping(value = "save")
	public String save(SerService serService, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, serService)){
			return form(serService, model);
		}
		serServiceService.save(serService);
		addMessage(redirectAttributes, "保存服务成功");
		return "redirect:"+Global.getAdminPath()+"/service/serService/?repage";
	}
	
	@RequiresPermissions("service:serService:edit")
	@RequestMapping(value = "delete")
	public String delete(SerService serService, RedirectAttributes redirectAttributes) {
		serServiceService.delete(serService);
		addMessage(redirectAttributes, "删除服务成功");
		return "redirect:"+Global.getAdminPath()+"/service/serService/?repage";
	}

}